package com.young.bs.vote.web;

import com.young.bs.vote.model.VoteResult;
import com.young.bs.vote.service.IVoteResultService;
import com.young.bs.vote.service.IVoterService;
import com.young.common.client.ums.UmsClientHelper;
import com.young.common.util.Result;
import com.young.common.util.StringUtils;
import com.young.interfaces.log.annotation.Log;
import com.young.interfaces.ums.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 投票
 *
 * @author xxx
 * @date 2019/3/27
 */
@Controller
@RequestMapping("/bs/vote/voter")
public class VoterController {
    @Resource
    IVoterService voterService;

    /**
     * 发起投票
     * @param voteId
     * @param optionId
     * @param request
     * @return
     */
    @Log("发起投票")
    @RequestMapping(value = "/initiate", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object initiate(String voteId, String optionId, HttpServletRequest request) {
        if (StringUtils.isBlank(voteId) || StringUtils.isBlank(optionId)) {
            return Result.buildFail("参数无效");
        }
        User user = UmsClientHelper.getLoginUser();
        if (user == null) {
            return Result.buildFail("登录无效");
        }
        voterService.initiate(voteId, optionId, user.getId());
        return new Result();
    }

    /**
     * 管理员认证
     * @param resultId
     * @param request
     * @return
     */
    @Log("管理员认证")
    @RequestMapping(value = "/approve", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object approve(String resultId, HttpServletRequest request) {
        if (StringUtils.isBlank(resultId)) {
            return Result.buildFail("参数无效");
        }
        User user = UmsClientHelper.getLoginUser();
        if (user == null) {
            return Result.buildFail("登录无效");
        }
        voterService.approve(resultId, user.getId());
        return new Result();
    }

    /**
     * 计票员计票
     * @param resultId
     * @param request
     * @return
     */
    @Log("计票员计票")
    @RequestMapping(value = "/counting", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object counting(String resultId, HttpServletRequest request) {
        if (StringUtils.isBlank(resultId)) {
            return Result.buildFail("参数无效");
        }
        User user = UmsClientHelper.getLoginUser();
        if (user == null) {
            return Result.buildFail("登录无效");
        }
        voterService.counting(resultId, user.getId());
        return new Result();
    }
}
